package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.sys.entity.SysMailLogEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysMailLogService;
import com.platform.modules.sys.service.SysUserService;

/**
 * 邮件发送日志Controller
 *
 * @author 肖亮
 */
@Api(tags = "SysMailLogController|邮箱配置管理")
@RestController
@RequestMapping("sys/maillog")
public class SysMailLogController extends AbstractController {
	@Autowired
	private SysMailLogService sysMailLogService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 分页查询邮件发送日志
	 *
	 * @param params
	 *            查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "分页查询邮件发送日志", notes = "分页查询邮件发送日志")
	@ApiImplicitParams({
	@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
	@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
    @ApiImplicitParam(paramType = "query", name = "sender", value = "发送人",required = false, dataType = "String")
    })
	@PostMapping("/list")
	//@RequiresPermissions("sys:maillog:list")
	public RestResponse list(Integer currPage,Integer limit,String sender) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 只能查看权限下的发送记录
			params.put("dataScope", getDataScope());
			params.put("sender", sender);
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			IPage page = sysMailLogService.queryPage(params);

		return RestResponse.success().put("page", page);
	}

	/**
	 * 根据主键查询详情
	 *
	 * @param id
	 *            主键
	 * @return RestResponse
	 */
	@ApiOperation(value = "根据主键查询详情", notes = "根据主键查询详情")
	@ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = false, dataType = "String")
    })
	@PostMapping("/info/{id}")
	@RequiresPermissions("sys:maillog:info")
	public RestResponse info(String id) {
		SysMailLogEntity sysMailLog = sysMailLogService.getById(id);

		return RestResponse.success().put("maillog", sysMailLog);
	}

	/**
	 * 根据主键删除邮件发送日志
	 *
	 * @param ids
	 *            ids
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除邮件发送日志", notes = "根据id删除模型（支持多id删除）")
	@SysLog("删除邮件发送日志")
	@PostMapping("/delete")
	@RequiresPermissions("sys:maillog:delete")
	public RestResponse delete(String[] ids) {
		sysMailLogService.deleteBatch(ids);
		return RestResponse.success();
	}

	/**
	 * 邮箱配置
	 *
	 * @param user
	 *            user
	 * @return RestResponse
	 */
	@ApiOperation(value = "邮箱配置", notes = "邮箱配置")
	@SysLog("修改邮箱配置")
	@PostMapping("/config")
	@RequiresPermissions("sys:maillog:config")
	public RestResponse config(SysUserEntity user) {

		sysUserService.update(user, new UpdateWrapper<SysUserEntity>().eq("USER_ID", user.getUserId()));

		return RestResponse.success();
	}
}
